//
//  WLCloudMoreView.m
//  WanLian
//
//  Created by muzhuang on 2018/10/19.
//  Copyright © 2018年 wanlian. All rights reserved.
//

#import "WLCloudMoreView.h"

@interface WLCloudMoreView()

@property (nonatomic, strong) UIView    *bgView;
@property (nonatomic,copy) void(^action)(NSInteger index);

@end

@implementation WLCloudMoreView


+ (instancetype)showMoreViewTitle:(NSString *)fileName action:(void(^)(NSInteger index))action
{
    WLCloudMoreView *moreView =[[[self class] alloc] initWithTitle:fileName action:action];
    return moreView;
}

- (instancetype)initWithTitle:(NSString *)filename action:(void(^)(NSInteger index))action
{
    self = [super init];
    if (self) {
        _action = action;
        self.frame = [[UIScreen mainScreen]bounds];//CGRectMake(0, 0, WIDTH_SCREEN_WL, HEIGHT_SCREEN_WL);
        //    背景，带灰度
        self.backgroundColor = wlRGBACOLOR(0, 0, 0, 0.5);
        //    可点击
        self.userInteractionEnabled = YES;
        //    点击背景，收起底部分享面板，移除本视图
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        [self addGestureRecognizer:tapGesture];
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT_SCREEN_WL, WIDTH_SCREEN_WL, WLScaleX(222)+WL_Bottom)];
        _bgView.backgroundColor = wlUIColorRGB(0xFAFAFA);
        [self addSubview:_bgView];
        UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(30, WLScaleX(10), WIDTH_SCREEN_WL-60, WLScaleX(30))];
        labTitle.font = [WLCommonTool fontTypePingFangSCRegular:17];
        labTitle.textColor = wlBlackColor;
        labTitle.textAlignment = NSTextAlignmentCenter;
        labTitle.text = filename;
        [_bgView addSubview:labTitle];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(60), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(1))];
        lineView.backgroundColor = wlUIColorRGB(0xE5E5E5);
        [_bgView addSubview:lineView];
        
        NSArray *arrImgs = @[@"cloud_flieDown",@"cloud_fileDelete",@"cloud_rename",@"cloud_fileDetail"];
        NSArray *names = @[@"下载到手机",@"删除",@"重命名",@"详细信息"];
        CGFloat space = (WIDTH_SCREEN_WL-4*WLScaleX(55))/5.0;
        CGFloat labSpace = (WIDTH_SCREEN_WL-4*WLScaleX(64))/5.0;
        for (int i=0; i<arrImgs.count; i++) {
            UIButton *btnAction = [[UIButton alloc] initWithFrame:CGRectMake(space*(i+1)+i*WLScaleX(55), WLScaleX(60+16), WLScaleX(55), WLScaleX(55))];
            btnAction.backgroundColor = wlUIColorRGB(0xE5E5E5);
            [btnAction setImage:[UIImage imageNamed:arrImgs[i]] forState:UIControlStateNormal];
            btnAction.tag = i;
            [btnAction addTarget:self action:@selector(moreBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [_bgView addSubview:btnAction];
            btnAction.layer.cornerRadius = WLScaleX(27.5);
            btnAction.layer.masksToBounds = YES;
            
            UILabel *labName = [[UILabel alloc] initWithFrame:CGRectMake(labSpace*(i+1)+i*WLScaleX(64), WLScaleX(60+16+55+5), WLScaleX(64), WLScaleX(20))];
            labName.font = [WLCommonTool fontTypePingFangSCRegular:12];
            labName.textColor = wlDarkGrayColor;
            labName.textAlignment = NSTextAlignmentCenter;
            labName.text = names[i];
            [_bgView addSubview:labName];
        }
        
        UIView *lineTwo = [[UIView alloc] initWithFrame:CGRectMake(WLScaleX(16), WLScaleX(175), WIDTH_SCREEN_WL-WLScaleX(32), WLScaleX(1))];
        lineTwo.backgroundColor = wlUIColorRGB(0xE5E5E5);
        [_bgView addSubview:lineTwo];
        
        UIButton *btnAction = [[UIButton alloc] initWithFrame:CGRectMake(20, WLScaleX(176), WIDTH_SCREEN_WL-40, WLScaleX(46))];
        btnAction.titleLabel.font = [WLCommonTool fontTypePingFangSCRegular:14];
        [btnAction setTitleColor:wlBlackColor forState:UIControlStateNormal];
        [btnAction setTitle:@"取消" forState:UIControlStateNormal];
        [btnAction addTarget:self action:@selector(tappedCancel) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:btnAction];
        
//        [self.bgView addSubview:self.cancelBtn];
//        [self.bgView addSubview:self.line];
        //share_cancel
//        NSArray *imageArray = [NSArray arrayWithObjects:@"share_sina",@"share_friend",@"share_wechat",@"share_qq", nil];
//        NSArray *arrayTitle = [NSArray arrayWithObjects:@"微博好友",@"朋友圈",@"微信好友",@"QQ好友", nil];
//
//        for (NSInteger i = 0; i < imageArray.count; i++) {
//
//            CGFloat x = self.bgView.bounds.size.width / 4 * ( i % 4);
//            CGFloat y = 0;
//            CGFloat w = self.bgView.bounds.size.width / 4;
//            CGFloat h = GIRTLX(336);
//
//            CGRect frame =  CGRectMake(x, y, w, h);
//            GIRTLImageWithLabel *item = [GIRTLImageWithLabel imageLabelWithFrame:frame Image:[UIImage imageNamed:imageArray[i]] LabelText:arrayTitle[i]];
//            item.labelOffsetY = GIRTLX(96);
//            item.labelFont = HPGIRTLFont(40);
//            item.labelColor = GRLBlackColor;
//
//            item.tag = 200 + i;
//            UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareAction:)];
//            [item addGestureRecognizer:tapGes];
//            [self.topSheetView addSubview:item];
//            [self.buttons addObject:item];
//        }
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        [window addSubview:self];
        //    弹出
        [UIView animateWithDuration:0.25f animations:^{
            self.bgView.frame = CGRectMake(0, HEIGHT_SCREEN_WL - CGRectGetHeight(self.bgView.frame), WIDTH_SCREEN_WL, CGRectGetHeight(self.bgView.frame));
        }];
        
    }
    return self;
}

- (void)tappedCancel
{
    [UIView animateWithDuration:0.25f animations:^{
        [self.bgView setFrame:CGRectMake(0, HEIGHT_SCREEN_WL, WIDTH_SCREEN_WL, 0)];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)moreBtnAction:(UIButton *)moreBtn
{
    if (self.action) {
        self.action(moreBtn.tag);
    }
    [self tappedCancel];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
